/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName VersionChecker.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model.utility
{
	
	import com.gesturesoft.imultimax.model.DataBaseManager;
	import com.gesturesoft.imultimax.model.cardmanager.CardDBManager;
	import com.gesturesoft.imultimax.model.cardmanager.ICardDBManager;
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.interfaces.IDatabaseManager;
	import com.gesturesoft.imultimax.model.thememanger.ThemeManager;
	import com.gesturesoft.imultimax.model.vo.AppVO;
	import com.gesturesoft.imultimax.model.vo.CardTemplateXMLVO;
	import com.gesturesoft.imultimax.model.vo.ThemeXMLDetailVO;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;

	public class VersionChecker
	{
		
		public static const COMPANY_DETAILS:String = "company_details";
		public static const THEME_DETAILS:String = "theme_details";
		
		private var _items4VersionUpdation:ArrayCollection;
		
		/**
		 * @Return array collection contains asset details for download
		 * @param configData config xml contain asset details
		 * 
		 */ 
		
		public static function check(configData : XML):Array
		{
			
			var versionChangedItems:Array = new Array();
			
			var companyDetails:ArrayCollection = new ArrayCollection();
			var themeXMLDetailVO:ThemeXMLDetailVO;
			
			for each(var item:XML in configData.application)
			{
				
				var appServerVO:AppVO = new AppVO();
				var databaseManager:IDatabaseManager = new DataBaseManager();
				
				//------------------------------------START:Add version changed company--------------------------------------------
				//Get version for the curresponding name
				var arr1:ArrayCollection = retrieveHandler("SELECT cmp_version FROM IM_COMPANIES WHERE cmp_company='"+item.name.toString()+"'");
				
				if(arr1.length>0) //This is a new entry
				{
					
					if(item.version.toString() != arr1[0].cmp_version)
					{   
						
						companyDetails = addUnversionedCompany(item, companyDetails, "edit");
						
					}
					
				}
				else //Check for any version change
				{
					
					companyDetails = addUnversionedCompany(item, companyDetails, "add");
					
				}
				
			}
			versionChangedItems[COMPANY_DETAILS] = companyDetails;
			//------------------------------------END:Add version changed company--------------------------------------------
			
			//------------------------------------START:Add version changed themes--------------------------------------------
			if( new ThemeManager().checkThemeVersion( configData.theme.@version.toString() ) )
			{
				
				themeXMLDetailVO = new ThemeXMLDetailVO();
				
				themeXMLDetailVO.versionChanged = true;
				themeXMLDetailVO.themeVersion = configData.theme.@version.toString();
				themeXMLDetailVO.url = "http://www.micmacsolutions.com/gesture_soft/themes1.xml";
				
				versionChangedItems[THEME_DETAILS] = themeXMLDetailVO;
				
			}
			//------------------------------------END:Add version changed themes----------------------------------------------
			
			//------------------------------------START:Add version changed card template details--------------------------------------------
			/*var cardTemplateVersionchanged:Boolean = false;
			var iCardDBManager:ICardDBManager = new CardDBManager();
			var cardTemplateXMLVO:CardTemplateXMLVO = null;
			var serverCardTemplateVersion:String;
			
			try
			{
				serverCardTemplateVersion = configData.cardTemplate.@version.toString();
			} 
			catch(error:Error) 
			{
				
				throw error;
				
			}
			
			cardTemplateVersionchanged = iCardDBManager.checkCardTemplateVersion(serverCardTemplateVersion);
			if(cardTemplateVersionchanged)
			{
				
				cardTemplateXMLVO = new CardTemplateXMLVO();
				cardTemplateXMLVO.versionChanged = true;
				cardTemplateXMLVO.cardTemplateVersion = serverCardTemplateVersion;
				cardTemplateXMLVO.url = "http://www.micmacsolutions.com/gesture_soft/card_template.xml";
				
				versionChangedItems[CARD_TEMPLATE_VERSION_DETAILS] = cardTemplateXMLVO;
				
			}*/
			//------------------------------------END:Add version changed card template details----------------------------------------------
			
			
			return versionChangedItems;
			
		}
		
		/**
		 * This is the function for checking the theme version change
		 * 
		 * @functionName isThemeVersionChanged
		 * @creationDate Aug 20, 2012
		 * @author micmac
		 * @params serverThemeVersion:String
		 */
		private static function themeVersionChanged(serverThemeVersion:String):Boolean
		{
			
			var iDataBaseManger:IDatabaseManager = new DataBaseManager();
			var arrTheme:ArrayCollection;
			var query:String = "SELECT * FROM THEME_VERSION WHERE theme_version='"+serverThemeVersion+"'";
			var versionChanged:Boolean = false;
			
			try
			{
				
				arrTheme = iDataBaseManger.getData( query );
				
			} 
			catch(error:DataBaseError) 
			{
				
				throw error;
				trace("error  -> ThemeManger.checkThemeVersion()");
				
			}
			
			if(arrTheme.length > 0)
			{
				
				if(serverThemeVersion != arrTheme[0].theme_version )
				{
					versionChanged = true;
				}
				
				
			}
			else
			{
				
				versionChanged = true;
				
			}
			
			return versionChanged;
			
		}
		
		/**
		 * @Return array collection contains asset details for download
		 * @param configData config xml contain asset details
		 * 
		 */ 
		
		/*public static function check(configData : XML):ArrayCollection
		{
			
			var items4VersionUpdation:ArrayCollection = new ArrayCollection();
			
			for each(var item:XML in configData.application)
			{
				
				var appServerVO:AppVO = new AppVO();
				var databaseManager:IDatabaseManager = new DataBaseManager();
				//Get version for the curresponding name
				//databaseManager.prepareTable("companies");
				var arr1:ArrayCollection = retrieveHandler("SELECT cmp_version FROM IM_COMPANIES WHERE cmp_company='"+item.name.toString()+"'");
				
				if(arr1.length>0) //This is a new entry
				{
					
					if(item.version.toString() != arr1[0].cmp_version)
					{   
						
						items4VersionUpdation = addItems4VersionUpdation(item, items4VersionUpdation, "edit");
						
					}
					
				}
				else //Check for any version change
				{
					
					items4VersionUpdation = addItems4VersionUpdation(item, items4VersionUpdation, "add");
					
				}
				
			}
			
			return items4VersionUpdation;
			
		}*/
		
		/**
		 * 
		 * Add updation details to an arraycollection
		 * @param item xml node curresponding to a company
		 * @mode specify whether it is used for a new addition or updating an existing one
		 */
		private static function addUnversionedCompany(item : XML,items4VersionUpdation:ArrayCollection, mode:String ):ArrayCollection
		{
			
			var obj:AppVO = new AppVO();
			obj.version = item.version.toString();
			obj.name = item.name.toString();
			obj.server = item.server.toString();
			obj.configFile = item.configFile.toString();
			obj.isEnable = Boolean( item.isEnable.toString() );
			obj.startUp = Number( item.startUp.toString() );
			obj.assetVersion = item.assetVersion.toString();
			obj.styleVersion = item.styleVersion.toString();
			obj.currency = item.currency.toString();
			obj.configFileSize = item.configFileSize.toString();;
			obj.mode = mode;
			items4VersionUpdation.addItem(obj);
			return items4VersionUpdation;
			
		}
		
		private static function retrieveHandler(val:String):ArrayCollection
		{
			
			var result:ArrayCollection;
			
			var _iDBManager:IDatabaseManager = new DataBaseManager();
			try
			{
				
				result = ArrayCollection(_iDBManager.getData(val));
				
			} 
			catch(error:DataBaseError) 
			{
				
				trace("Application-retrieveHandler() : Database Read error");
				throw error;
				
			}
			
			return result;
			
		}
		
	}
	
}